﻿using System.Diagnostics;
using HueLampStatusDetector.Status;
using SKYPE4COMLib;

namespace HueLampStatusDetector.StatusChecker
{
    public class SkypeStatusChecker : IStatusChecker
    {
        private Skype _skype;

        public SkypeStatusChecker()
        {
            _skype = new Skype();
            _skype.Client.Start(true, true);
            _skype.Attach(6, true);

            _skype.CallStatus += OnCallStatusChange;
        }

        public ChangeStatus ChangeStatus { get; set; }

        private void OnCallStatusChange(Call pCall, TCallStatus status)
        {
            Trace.TraceInformation("Skype statusType: " + status);
            if (status == TCallStatus.clsInProgress)
            {
                SendNotification(StatusType.Busy);
                return;
            }

            if (status == TCallStatus.clsFinished)
            {
                SendNotification(StatusType.Free);
                return;
            }
        }

        private void SendNotification(StatusType statusType)
        {
            if (ChangeStatus != null)
            {
                ChangeStatus.Invoke(this, new ChangeStatusEventArgs(statusType));
            }
        }
    }
}